/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.base;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

import cn.com.ksplatform.base.adapter.IRequestParamAdapter;
import cn.com.ksplatform.base.adapter.RuleNotFondException;
import cn.com.ksplatform.base.page.PageInfoModel;
import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.common.util.BaseDaoUtil;

/**
 * 控制器基类 系统所有的控制器都需要基础该类 BaseController
 * 
 * 2015年3月17日 上午9:39:06
 * 
 * @author Mr.Xiao
 * @version 1.0.0
 * 
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class BaseController {

	@Autowired
	private HttpServletRequest request;

	BaseService baseService;
	
	
	@Autowired
	@Qualifier("paramAdapters.hqlRequestParamAdapter")
	private IRequestParamAdapter requestParamAdapter;
	
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public Return load(Class clazz,Serializable id){
		return SimpleReturn.success("操作成功!",baseService.get(clazz, id));
	}
	
	public Return findAll(Class clazz) throws Exception {
		Map orderMap = new HashMap();
		if (request.getParameter("sortName")!=null) {
			orderMap.clear();
			orderMap.put("sortName", request.getParameter("sortName"));
			orderMap.put("sortValue", request.getParameter("sortValue"));
		}
		try {
			return SimpleReturn.success("操作成功!",baseService.findAll(clazz, requestParamAdapter.doAdapter(request,BaseDaoUtil.getRowMapper(clazz)),orderMap));
		} catch (RuleNotFondException e) {
			return SimpleReturn.success("操作成功!",baseService.findAll(clazz, null,orderMap));
		}
		
	}
	
	public PageInfoModel queryPage(Class clazz) throws Exception {
		PageInfoModel pim = new PageInfoModel();
		initPage(pim, request);
		try {
			pim = baseService.queryPage(pim, clazz, requestParamAdapter.doAdapter(request,BaseDaoUtil.getRowMapper(clazz)));
		} catch (RuleNotFondException e) {
			pim = baseService.queryPage(pim, clazz, null);
		}
		
		return pim;
	}

	/**
	 * 
	 * initPage:翻页参数. <br/>
	 * @author Mr.Luoxing
	 * @param multiply
	 * @param pim
	 * @param request
	 */
	public void initPage(PageInfoModel pim, HttpServletRequest request) {
		int currentPage = 1;
		int totalRow = 15;
		String str_currentPage = request.getParameter("page");
		currentPage = str_currentPage == null || "".equals(str_currentPage) ? 1 : Integer.parseInt(str_currentPage);
		String str_pageRow = request.getParameter("rows");
		totalRow = str_pageRow == null || "".equals(str_pageRow) ? 15 : Integer.parseInt(str_pageRow);
		
		String str_sortName = request.getParameter("sortName");
		String str_sortOrder = request.getParameter("sortOrder");
		
		if(StringUtils.hasText(request.getParameter("sortValue"))){
			str_sortOrder = request.getParameter("sortValue");
		}
		pim.setSortName(str_sortName);
		pim.setSortOrder(str_sortOrder);
		pim.setPage(currentPage);
		pim.setPageSize(totalRow);
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public BaseService getBaseService() {
		return baseService;
	}
}
